package com.chixing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chixing.entity.Dish;
import com.chixing.mapper.DishMapper;
import com.chixing.service.IDishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author smith
 * @since 2025-05-21
 */

//
@Service
public class DishServiceImpl implements IDishService {
//调用持久层
@Autowired
private  DishMapper dishMapper;
    @Override
    public Result getById(Integer dishId) {
        Dish dish = dishMapper.selectById(dishId);
        if(dish != null)
            return  Result.getSuccess(dish);
        return Result.getFail(null);
    }
    @Override
    public Result getAll() {
        //select * from dish
        List<Dish> dishList = dishMapper.selectList(null);
        if(dishList !=null && dishList.size() >0)
            return Result.getSuccess(dishList);
        else
            return Result.getFail("暂无菜品数据");
    }
    @Override
    public Result getByTypeId(Integer typeId) {
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        wrapper.eq("type_id",typeId);
        //select * from dish where type_id = ?
        List<Dish> dishList = dishMapper.selectList(wrapper);
        if(dishList !=null && dishList.size() >0)
            return Result.getSuccess(dishList);
        else
            return Result.getFail("暂无该类别的菜品");
    }

    @Override
    public Result getByName(Integer name) {
        return null;
    }

    @Override
    public Result getByType_Name(Integer typeId, Integer name) {
        return null;
    }

    @Override
    public  Result save(Dish dish){
        // insert into dish ...
        //rows 表示dish表中受影响行数 , rows = 1 表示 添加了一条记录成功
        int rows = dishMapper.insert(dish);
        if (rows >0)
            return Result.saveSuccess(dish);
        return Result.saveFail(dish);
    }
    @Override
    public  Result update(Dish dish){
        dish.setUpdateTime(LocalDateTime.now());
        // update dish set ...... where dish_id = ?
        int rows = dishMapper.updateById(dish);
        if (rows >0)
            return Result.updateSuccess(dish);
        return  Result.updateFail(dish);
    }
    public Result delete(Integer dishId){
        int rows = dishMapper.deleteById(dishId);
        if (rows >0)
            return Result.deleteSuccess(dishId);
        return  Result.deleteFail(dishId);
    }

    public Result getByName(String name) {
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        wrapper.like("dish_name", name);
        List<Dish> dishList = dishMapper.selectList(wrapper);
        if(dishList != null && !dishList.isEmpty()) {
            return Result.getSuccess(dishList);
        } else {
            return Result.getFail("未找到相关菜品");
        }
    }


    public Result getByTypeAndName(Integer typeId, String name) {
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        wrapper.eq("type_id", typeId)
                .like("dish_name", name);
        List<Dish> dishList = dishMapper.selectList(wrapper);
        if(dishList != null && !dishList.isEmpty()) {
            return Result.getSuccess(dishList);
        } else {
            return Result.getFail("未找到相关菜品");
        }
    }
}

